package cn.sollyu.SDManager;

import java.io.File;
import java.util.HashMap;

import cn.sollyu.SDManger.R;

import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.Preference.OnPreferenceClickListener;
import android.view.Menu;
import android.view.MenuItem;

public class EditActivityPre extends PreferenceActivity implements
		OnPreferenceChangeListener, OnPreferenceClickListener
{
	private HashMap<String, Object> nowSelectHashMap;
	Preference infoPreference;
	EditTextPreference infoEditTextPreference ;
	EditTextPreference softEditTextPreference;
	EditTextPreference deleEditTextPreference;
	ListPreference listPreference;
	
	@SuppressWarnings("unchecked")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		addPreferencesFromResource(R.xml.edit_activity);
		
		getPreferenceManager().setSharedPreferencesName("temp");
		
		nowSelectHashMap = (HashMap<String, Object>) getIntent().getSerializableExtra(getString(R.string.main_activity_2_edit_activity_Extra));
		
		infoPreference = findPreference(getString(R.string.edit_activity_key_name));
		infoEditTextPreference = (EditTextPreference) findPreference(getString(R.string.edit_activity_key_info));
		softEditTextPreference = (EditTextPreference) findPreference(getString(R.string.edit_activity_key_soft));
		deleEditTextPreference = (EditTextPreference) findPreference(getString(R.string.edit_activity_key_dele));
		listPreference = (ListPreference)findPreference(getString(R.string.edit_activity_key_index));
		
		infoPreference.setTitle((String)nowSelectHashMap.get(getString(R.string.hashMapName)));
		infoPreference.setSummary(new File((String)nowSelectHashMap.get(getString(R.string.hashMapPath))).getParent());
		infoPreference.setOnPreferenceChangeListener(this);
		
		String infoSummaryString = (String)nowSelectHashMap.get(getString(R.string.hashMapInfo));
		if (infoSummaryString  != null && infoSummaryString.length() > 1)
		{
			infoEditTextPreference.setSummary(infoSummaryString);
			infoEditTextPreference.setText((String)nowSelectHashMap.get(getString(R.string.hashMapInfo)));
		}
		infoEditTextPreference.setOnPreferenceChangeListener(this);
		
		
		String parentString = new File(nowSelectHashMap.get(getString(R.string.hashMapPath)).toString()).getParent(),softSummaryString;
		if (parentString.equals(Environment.getExternalStorageDirectory().toString()))
		{
			softSummaryString = (String) nowSelectHashMap.get(getString(R.string.hashMapSoftware));
			
		} else
		{
			softSummaryString = new DatabaseManagerService(this).getSoftString(parentString);
		}
		
		if (softSummaryString != null && softSummaryString.length() > 1)
		{
			softEditTextPreference.setText(softSummaryString);
			softEditTextPreference.setSummary(softSummaryString);
		}
		softEditTextPreference.setOnPreferenceChangeListener(this);
					
		
		
		String deleSummaryString  = (String)nowSelectHashMap.get(getString(R.string.hashMapDel)); 
		if (deleSummaryString != null && deleSummaryString.length() > 1)
		{
			deleEditTextPreference.setSummary(deleSummaryString);
			deleEditTextPreference.setText((String)nowSelectHashMap.get(getString(R.string.hashMapDel)));
		}
		deleEditTextPreference.setOnPreferenceChangeListener(this);
		
		Integer nowSelectInteger = new Integer((String)nowSelectHashMap.get(getString(R.string.hashMapIndex)))-1;
		if (nowSelectInteger > 0)
		{
			String listSummary =  getResources().getStringArray(R.array.edit_activity_spinner_index)[nowSelectInteger];
			listPreference.setSummary(listSummary);
			listPreference.setValueIndex(nowSelectInteger);
		}
		listPreference.setOnPreferenceChangeListener(this);
	}

	@Override
	public boolean onPreferenceClick(Preference arg0) {
		// TODO Auto-generated method stub
		return false;
	}
	
	@Override
	public boolean onPreferenceChange(Preference preference, Object newValue) {
		// 信息
		if (preference.getKey().equals(getString(R.string.edit_activity_key_info)))
		{
			infoEditTextPreference.setSummary(newValue.toString());
		}
		// 生成软件
		else if (preference.getKey().equals(getString(R.string.edit_activity_key_soft)))
		{
			softEditTextPreference.setSummary(newValue.toString());
		}
		// 强制删除
		else if (preference.getKey().equals(getString(R.string.edit_activity_key_dele)))
		{
			deleEditTextPreference.setSummary(newValue.toString());
		}
		 // 删除指数
		else if (preference.getKey().equals(getString(R.string.edit_activity_key_index)))
		{
			String listSummary =  getResources().getStringArray(R.array.edit_activity_spinner_index)[new Integer(newValue.toString())];
			listPreference.setSummary(listSummary);
		}
		
		
		return true;
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		
		menu.add(R.string.edit_activity_button_save_lable).setIcon(android.R.drawable.ic_menu_edit);
		menu.add(R.string.edit_activity_button_cancel_lable).setIcon(android.R.drawable.ic_menu_close_clear_cancel);
		
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		
		DatabaseManagerService databaseManagerService = new DatabaseManagerService(this);
		String fileUriString = (String)nowSelectHashMap.get(getString(R.string.hashMapUri));
//		Toast.makeText(this, listPreference.getValue(), 1).show();
		
		if (item.toString().equals(getString(R.string.edit_activity_button_save_lable)))
		{
			if (getString(R.string.database_info_not_fond).equals(databaseManagerService.getInfoString(nowSelectHashMap.get(getString(R.string.hashMapPath)).toString())))
			{
				
				databaseManagerService.addNewInfo(
						nowSelectHashMap.get(getString(R.string.hashMapPath)).toString(),
						nowSelectHashMap.get(getString(R.string.hashMapName)).toString(),
						infoEditTextPreference.getText(),
						softEditTextPreference.getText(), 
						deleEditTextPreference.getText(),
						new Integer( listPreference.getValue()) + 1,
						fileUriString);
			}else {
				databaseManagerService.updateOneInfo(
						nowSelectHashMap.get(getString(R.string.hashMapPath)).toString(), 
						nowSelectHashMap.get(getString(R.string.hashMapName)).toString(),
						infoEditTextPreference.getText(),
						softEditTextPreference.getText(), 
						deleEditTextPreference.getText(),
						new Integer( listPreference.getValue()) + 1,
						fileUriString);
			}
			
			Intent intent = new Intent();
			intent.putExtra(getString(R.string.edit_activity_save_status_result_title), getString(R.string.edit_activity_save_status_result_success));
			setResult(11, intent);
			finish();
		}else {
			Intent intent = new Intent();
			intent.putExtra(getString(R.string.edit_activity_save_status_result_title), getString(R.string.edit_activity_save_status_result_falure));
			setResult(11, intent);
			finish();
		}

		return super.onOptionsItemSelected(item);
	}

	
}
